package com.winkyle.woodenfish.util;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.linchaolong.android.imagepicker.ImagePicker;
import com.linchaolong.android.imagepicker.cropper.CropImage;
import com.linchaolong.android.imagepicker.cropper.CropImageView;


import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

/**
 *
 */
/**
 * todo
 * Created by winkyle on 2018/1/21.
 * @author winkyle
 * ImagePickerUtil自定义图片选择器工具,使用图片选择器需配置
 * <activity
 *     android:name="com.linchaolong.android.imagepicker.cropper.CropImageActivity"
 *     android:theme="@style/Base.Theme.AppCompat"/>
 */
public class ImagePickerUtil {
    private ImagePicker imagePicker;


    public ImagePickerUtil(String title){
        imagePicker = new ImagePicker();
        // 设置标题
        imagePicker.setTitle(title);
    }

    /**
     * 设置是否裁剪图片
     * @param cropImage
     */
    public void setCropImage(boolean cropImage){
        imagePicker.setCropImage(cropImage);
    }


    private File file;

    public File getFile() {
        return file;
    }

    /**
     * 设置选择一张图片回调
     */
    public void setPickeOneImage(){
        imagePickerCallback = new ImagePicker.Callback() {
            // 选择图片回调
            @Override public void onPickImage(Uri imageUri) {
                createImageFile(imageUri);
                if(imagePickerListener!=null)
                imagePickerListener.imagePickerSucceed(imageUri);
            }

            // 裁剪图片回调
            @Override public void onCropImage(Uri imageUri) {
                createImageFile(imageUri);
                if(imageCropListener!=null)
                imageCropListener.imageCropSucceed(imageUri);
            }

            // 自定义裁剪配置
            @Override public void cropConfig(CropImage.ActivityBuilder builder) {

                builder
                        // 是否启动多点触摸
                        .setMultiTouchEnabled(false)
                        // 圆形/矩形
                        .setCropShape(CropImageView.CropShape.OVAL)
                        // 调整裁剪后的图片最终大小（单位：px）
                        .setRequestedSize(300, 300)
                        // 裁剪框宽高比
                        .setAspectRatio(5, 5);
            }

            // 用户拒绝授权回调
            @Override public void onPermissionDenied(int requestCode, String[] permissions,
                                                     int[] grantResults) {
            }
        };
    }

    /**
     * 将图片生成文件
     * @param imageUri 要生成文件的图片Uri
     */
    public void createImageFile(Uri imageUri){
        try {
            file = new File(new URI(imageUri.toString()));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    private ImagePicker.Callback imagePickerCallback;
    public void startChooser(Activity activity){
        imagePicker.startChooser(activity,imagePickerCallback);
    }

    public void onActivityResult(Activity activity,int requestCode, int resultCode, Intent data){
        imagePicker.onActivityResult(activity, requestCode, resultCode, data);
    }

    public void onRequestPermissionsResult(Activity activity,int requestCode, String[] permissions,
                                           int[] grantResults){
        imagePicker.onRequestPermissionsResult(activity, requestCode, permissions, grantResults);
    }

    private ImagePickerListener imagePickerListener;
    public void setImagePickerListener(ImagePickerListener imagePickerListener){
        this.imagePickerListener = imagePickerListener;
    }

    public interface ImagePickerListener{
        /**
         * 图片选择成功
         * @param imageUri 选择的图片Uri
         */
        void imagePickerSucceed(Uri imageUri);
    }

    private ImageCropListener imageCropListener;
    public void setImageCropListener(ImageCropListener imageCropListener){
        this.imageCropListener = imageCropListener;
    }

    public interface ImageCropListener{
        /**
         * 图片裁剪成功
         * @param imageUri 裁剪的图片Uri
         */
        void imageCropSucceed(Uri imageUri);
    }

}
